package com.biwin.core.main.config;

import cn.hutool.core.collection.CollUtil;
import com.biwin.core.main.property.BiWinLoginProperties;
import com.biwin.core.main.property.BiWinProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ContentSpecificationBuilder;
import springfox.documentation.builders.ExampleBuilder;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.Operation;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @author biwin
 * @since 0.0.1
 */
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {

    @Autowired
    private BiWinProperties biWinProperties;
    @Autowired
    private BiWinLoginProperties loginProperties;

    /**
     * Implement this method to manually add ApiDescriptions
     *
     * @param context - Documentation context that can be used infer documentation context
     * @return List of {@link ApiDescription}
     * @see ApiDescription
     */
    @Override
    public List<ApiDescription> apply(DocumentationContext context) {
        ApiDescription loginApiDescription = new ApiDescription("BiWin Framework",
                loginProperties.getCustomLoginUrl(), "框架核心登录接口",
                "框架核心登录接口描述",
                Arrays.asList(buildLoginApi()), false);

        return Arrays.asList(loginApiDescription);
    }

    private Operation buildLoginApi() {
        return new OperationBuilder(new CachingOperationNameGenerator())
                .method(HttpMethod.POST)
                .summary("框架后台登录")
                .notes("默认使用username/password,如果在配置文件中修改过,则需按配置文件传参")
                .consumes(CollUtil.newHashSet(MediaType.APPLICATION_FORM_URLENCODED_VALUE))
                .produces(CollUtil.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .tags(CollUtil.newHashSet("登录"))
                .requestParameters(Arrays.asList(
                        new RequestParameterBuilder()
                                .description("用户名")
                                .name(loginProperties.getUsername())
                                .deprecated(false)
                                .required(true)
//                                .in(ParameterType.FORMDATA)
                                .parameterIndex(0)
                                .build(),
                        new RequestParameterBuilder()
                                .description("密码")
                                .name(loginProperties.getPassword())
                                .deprecated(false)
                                .required(true)
//                                .in(ParameterType.FORMDATA)
                                .parameterIndex(1)
                                .build()
                ))
                //                .parameters(Arrays.asList(
                //                        new ParameterBuilder()
                //                                .description("用户名")
                //                                .type(new TypeResolver().resolve(String.class))
                //                                .name("username")
                //                                .defaultValue("admin")
                //                                .parameterType("query")
                //                                .parameterAccess("access")
                //                                .required(true)
                //                                .modelRef(new ModelRef("string"))
                //                                .build()
                //                ))
                //                .responseMessages(Collections.singleton(
                //                        new ResponseMessageBuilder().code(200).message("请求成功")
                //                                .responseModel(new ModelRef(
                //                                        "xyz.gits.boot.common.core.response.RestResponse")
                //                                ).build()))
                .build();
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return DocumentationType.OAS_30.equals(documentationType);
    }
}
